<?php
/*
*	Kleophatra - Open Source Content Management System
*	------------------------------------------------------------------------
*	Copyright (C) 2007-2009, Sergio Milardovich <smilardovich@yahoo.com.ar>
*	This program is Free Software.
*
*	@package	Kleophatra
*	@subpackage	/lib
*	@license	http://www.gnu.org/copyleft/gpl.html GNU/GPL License 2.0
*	@author		Sergio Milardovich <smilardovich@yahoo.com.ar>
*	@link		http://www.kleophatra.org
*/
	class kleowidgets extends kleotemplate{

		var $widget_form = array();
		var $widget_places = array(
					   1 => array("TOP","top"),
					   2 => array("HEAD","head"),
					   3 => array("DH","downhead"),
					   4 => array("CU","center"),
					   5 => array("CD","center"),
					   6 => array("FOOTER","foot"),
					   7 => array("RIGHT","right"),
					   8 => array("LEFT","left"),
					   9 => array("SIDEBAR", "sidebar")
					 );
		
		// wid = 1=top,2=head,3=downhead,4=center up,5=center bottom,6=footer,7=right,8=left,9=sidebar
		function make_widget($buff, &$buff){
			foreach($this->widget_places as $wid => $position){

				$query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."widgets WHERE status=1 AND pid=$wid ORDER BY oid ASC");
			
				$widget_output = "\n";
				$lower_type = strtolower($position[0]);
				$upper_type = $position[0];
				$type = $position[1];


				if(file_exists(KLEO_FULL_TEMPLATE_ROUTE.'widgets/'.$type.'_widgets.tpl')){
					$tpl_file = $type.'_widgets.tpl';
				} else if(file_exists(KLEO_FULL_TEMPLATE_ROUTE.'widgets/default_widgets.tpl')){
					$tpl_file = 'default_widgets.tpl';
				}
				
				if($this->num_rows($query) == 0){
					$buff = str_replace("{WEB_".$upper_type."_WIDGETS}", $widget_output, $buff);
				} else {
					while($row = $this->fetch_array($query)){

					  if($this->check_widget_privilegies($row['wid'])){

						//Small hack that allows you to put a widget in more than one place at the same time.
						$_SESSION['widget_ref'][$row['script']] = (int)$row['ref'];

						$icon = $row['icon'];
						$title = $row['title'];
						$style = $row['style'];
						if(isset($tpl_file)){
							$this->tpl_load(KLEO_FULL_TEMPLATE_ROUTE.'widgets/'.$tpl_file, $wbuff);
						} else {
							$wbuff = null;
						}

						$this->tpl_set("WIDGET_POSITION", $lower_type, $wbuff, $wbuff);
						if(!$row['script'] == null){
							$include_file = explode("->", $row['script']);
							if($include_file[0] !== 'plugins'){
						  		$include_file = KLEO_SOURCE_DIR.'modules/'.$include_file[0].'/widgets/'.	$include_file[1].'.php';
							} else {
						  		$include_dir = explode("/", $include_file[1]);
						  		$include_file = KLEO_SOURCE_DIR.'plugins/'.$include_dir[0].'/widgets/'.	$include_dir[1].'.php';
							}
				
							if(!file_exists($include_file)){
								break;
							}
							ob_start();
 							require($include_file);
							$include_file = ob_get_contents();
							ob_end_clean();
							$widget_content = $row['html'].$output;
						} else {
							$widget_content = $row['html'];
						}
						$this->tpl_set("WIDGET_TITLE", $title, $wbuff, $wbuff);
						$this->tpl_set("WIDGET_CONTENT", $widget_content, $wbuff, $wbuff);
						$this->tpl_set("WIDGET_ICON", $icon, $wbuff, $wbuff);
						$this->tpl_set("WIDGET_CLASS", $style, $wbuff, $wbuff);
						$this->tpl_set("KLEO_ICONTHEME", KLEO_CURRENT_ICONS, $wbuff, $wbuff);
						$widget_output .= $wbuff;
						if(isset($output)){
							unset($output);
						}
					  }
					}
				}
			$this->tpl_set("WEB_".$upper_type."_WIDGETS", $widget_output, $buff, $buff);
			}

		}

		function make_footer($buff, &$buff){
			$get_footer = $this->get_core_value("web_footer", "name");
			$this->tpl_set("WEB_FOOTER", $get_footer, $buff, $buff);
		}
		
		function check_widget_privilegies($id){
			$check_query = $this->query("SELECT * FROM ".KLEO_TABLE_PREFIX."widgets_groups WHERE widgetid = $id LIMIT 1");
			$check_row = $this->fetch_array($check_query);
			if($check_row['sections'] !== '*'){
				$check_cut = explode(",",$check_row['sections']);
				foreach($check_cut as $check_item){
					if($check_item == KLEO_ACTUAL_MODULE){
						return true;
					}
				}
			} else {
				return true;
			}
		}
		function remove_widget($id){
			$this->query("DELETE FROM `".KLEO_TABLE_PREFIX."widgets` WHERE `wid` = $id LIMIT 1");
			$this->query("DELETE FROM `".KLEO_TABLE_PREFIX."widgets_groups` WHERE `wgid` = $id LIMIT 1");
		}
		function add_widget_group($widgetid,$sections,$id='NULL'){
			$this->query("INSERT INTO `".KLEO_TABLE_PREFIX."widgets_groups` (
				`wgid` ,
				`widgetid` ,
				`sections`
			) VALUES (
				$id , '$widgetid', '$sections'
			);");
		}
		function add_widget($title,$html,$script,$pid,$oid,$sections='*',$icon='default.png',$status=1,$css=null,$style=null){
			$widgetid = $this->get_last_widget_id();
			$ref = $widgetid;
			$this->query("INSERT INTO `".KLEO_TABLE_PREFIX."widgets` (
				`wid` ,
				`title` ,
				`icon` ,
				`html` ,
				`script` ,
				`pid` ,
				`style` ,
				`css` ,
				`oid` ,
				`status` ,
				`ref` ,
				`date_registered`
			) VALUES (
				NULL , '$title', '$icon', '$html', '$script', '$pid', '$css', '$style', '$oid', '$status', '$ref', CURRENT_TIMESTAMP
			);");
			kleowidgets::add_widget_group($widgetid,$sections);
		}
		function edit_widget($id,$args){
			if(empty($args['status'])){
				$args['status'] = 1;
			}
			$sections = $this->get_posted_sections();
			@$this->query("UPDATE `".KLEO_TABLE_PREFIX."widgets_groups` SET `sections` = '$sections' WHERE `wgid` =$id LIMIT 1 ;");
			foreach ($args as $name => $value){
				@$this->query("UPDATE `".KLEO_TABLE_PREFIX."widgets` SET `$name` = '$value' WHERE `wid` =$id LIMIT 1 ;");
				if($this->is_value($name,$id)){
					$this->set_core_value($name, $value, $id);
				}
			}
		}
	}

?>
